package com.jlwl.houseservice.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jlwl.houseservice.common.Result;
import com.jlwl.houseservice.entity.Article;
import com.jlwl.houseservice.entity.ArticleCategory;
import com.jlwl.houseservice.service.ArticleCategoryService;
import com.jlwl.houseservice.service.ArticleService;
import com.jlwl.houseservice.vo.ArticleDetailVO;
import com.jlwl.houseservice.vo.ArticleQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章控制器
 */
@RestController
@RequestMapping("/articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleCategoryService articleCategoryService;

    /**
     * 获取文章列表
     */
    @GetMapping
    public Result<Page<Article>> list(ArticleQueryVO queryVO) {
        Page<Article> page = articleService.pageList(queryVO);
        return Result.success(page);
    }

    /**
     * 获取文章详情
     */
    @GetMapping("/{id}")
    public Result<ArticleDetailVO> getDetail(@PathVariable Integer id) {
        // 增加浏览量
        articleService.incrementViewCount(id);
        
        // 获取文章详情
        ArticleDetailVO detailVO = articleService.getArticleDetail(id);
        if (detailVO == null) {
            return Result.error("文章不存在");
        }
        
        return Result.success(detailVO);
    }

    /**
     * 获取推荐文章列表
     */
    @GetMapping("/recommend")
    public Result<List<Article>> getRecommendArticles(@RequestParam(defaultValue = "5") Integer limit) {
        List<Article> list = articleService.getRecommendArticles(limit);
        return Result.success(list);
    }

    /**
     * 根据分类获取文章列表
     */
    @GetMapping("/category/{categoryId}")
    public Result<List<Article>> getArticlesByCategory(
            @PathVariable Integer categoryId,
            @RequestParam(defaultValue = "10") Integer limit) {
        List<Article> list = articleService.getArticlesByCategory(categoryId, limit);
        return Result.success(list);
    }

    /**
     * 获取所有文章分类
     */
    @GetMapping("/categories")
    public Result<List<ArticleCategory>> getAllCategories() {
        List<ArticleCategory> list = articleCategoryService.getAllEnabledCategories();
        return Result.success(list);
    }
} 